#!/bin/bash

# 查看用户是否登录
login_user=$(who | grep -i -m 1 $1 | awk '{print $1}')

if [ -z $login_user ]; then
    echo "$1 不在线！"
    echo "脚本退出.."
    exit
fi
# 查看用户是否开启消息功能
is_allowed=$(who -T | grep -i -m 1 $1 | awk '{print $1}')

if [ $is_allowed != "+" ]; then
    echo "$1 没有开启消息功能"
    echo "脚本退出.."
    exit
fi
# 确认是否有消息发送
if [ -z $2 ]; then
    echo "没有消息发送"
    echo "脚本退出"
    exit
fi
# 从参数中获取要发送的消息
whole_msg=$(who | grep -i -m $1 | awk '{print $1}')

# 写入要发送的消息
echo $whole_msg | write $login_user $user_terminal

if [ $? != 0 ]; then
    echo "发送失败！"
else
    echo "发送成功！"
fi
exit
